#!/usr/bin/env ruby

# Copyright (c) 2010-2012 Microsoft Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#	http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'rubygems'
require 'ruby-aws'
require 'optparse'

require './include/config'
require './include/proxy'
require './include/requester'

@config = MOS::Config.create
@mturk = Amazon::WebServices::MechanicalTurkRequester.new :Host => @config[:Server]

@options = { }
optparse = OptionParser.new do |opts|
	opts.banner = "Usage: #{$0} [options] <worker-id> <reason>"

	@options[:operation] = nil

	@options[:force] = false
	opts.on( '-f', '--force', 'Do not ask for confirmation' ) do
		@options[:force] = true
	end

	opts.on( '-b', '--block', 'Block a worker' ) do
		@options[:operation] = :block
	end

	opts.on( '-u', '--unblock', 'Unblock a worker' ) do
		@options[:operation] = :unblock
	end

	opts.on( '-h', '--help', 'Display this screen' ) do
		puts opts
		exit
	end

	opts.on_tail("")
	opts.on_tail("Allows or disallows a worker from submitting your HITs.")
	opts.on_tail("The block is account-wide, and is independent of qualifications.")
	opts.on_tail("Make sure to state the reason for your block/unblock.")
end

begin
	optparse.parse!
rescue OptionParser::ParseError => e
	$stderr.puts("Error parsing command-line arguments: " + e.message)
	$stderr.puts("Try '#{$0} --help' for more information.")
	exit
end

begin
	case @options[:operation]
		when :block
			raise "incorrect number of arguments" unless ARGV.length == 2
		when :unblock
			raise "incorrect number of arguments" unless ARGV.length == 2
		else
			raise "operation must be indicated"
	end
rescue => e
	$stderr.puts("Error: " + e.message + ".")
	$stderr.puts("Try '#{$0} --help' for more information.")
	exit
end

worker_id  = ARGV[0]
note       = ARGV[1]

if !@options[:force]
	if @options[:operation] == :block
		printf("WARNING: blocking users can lead to getting them banned by Amazon!\n");
		printf("Only block a user if you are absolutely sure they misbehaved, and state the reason.\n\n");
	end

	printf("Are you sure you want to %s WorkerId %s? (y/n) ", @options[:operation].to_s, worker_id)
	response = STDIN.gets.chomp.downcase
	if response != 'y'
		printf("Exiting...\n")
		exit
	end
end

begin
	case @options[:operation]
		when :block
			@mturk.block_worker(worker_id, note)
			printf("Blocked WorkerID %s.\n", worker_id)
		when :unblock
			@mturk.unblock_worker(worker_id, note)
			printf("Unblocked WorkerID %s.\n", worker_id)
	end
rescue => e
	$stderr.printf("Error: %s\n", e.message)
	exit
end

